import { app, BrowserWindow, Menu, ipcMain } from "electron"
import path from "path"

import { adbDevices, executeAdbLogcatWithPipe, adbStartApp, adbGetTopPackageName, adbGetWholeActivityFromPackageName, registerIpcMain } from './controller/adb.js'

const __dirname = path.resolve()

const createWindow = async () => {

    const win = new BrowserWindow({
        width: 1600,
        height: 1200,
        minWidth: 1200, // 最小宽度
        minHeight: 800, // 最小高度
        // resizable: false,//是否允许调整大小
        // frame: false,//去除windows原生边框
        show: false,
        webPreferences: {
            preload: path.resolve(__dirname, "./preload/index.js"), // 在预加载脚本中执行 Electron API
        },
    })

    win.loadURL('http://localhost:5173/')
    Menu.setApplicationMenu(null);

    win.once('ready-to-show', () => {
        win.show()
    })

    // win.webContents.openDevTools()
}

app.whenReady().then(() => {
    createWindow()
})

registerIpcMain()

ipcMain.handle('on-get-devices', async (event) => {
    const deviceIds = await adbDevices()
    return deviceIds
})

ipcMain.handle('on-get-package-name', async (event, snArray) => {
    return adbGetTopPackageName(snArray).then((results) => {
        console.log(results)
        return results
    }).catch((err) => {
        return err
    })
})

ipcMain.handle('on-get-whole-activity', async (event, sn, packageName) => {
    return adbGetWholeActivityFromPackageName(sn, packageName).then((wholeActivity) => {
        console.log(wholeActivity)
        return wholeActivity
    }).catch((err) => {
        return err
    })
})

